﻿using UnityEngine;
using System.Collections;
using admob;

public class AdmobWidget : Singleton<AdmobWidget> {
	Admob ad;

	public string Banner = "ca-app-pub-3471939276113753/3813288026";
	public string Interstitial = "ca-app-pub-3471939276113753/5290021224";

	void Start () {
		initAdmob();
	}
	void initAdmob()
	{

		//  isAdmobInited = true;
		ad = Admob.Instance();
		ad.bannerEventHandler += onBannerEvent;
		ad.interstitialEventHandler += onInterstitialEvent;
		ad.rewardedVideoEventHandler += onRewardedVideoEvent;
		ad.nativeBannerEventHandler += onNativeBannerEvent;
		ad.initAdmob(Banner, Interstitial); //Banner,Interstitial
		//   ad.setTesting(true);
		ad.setGender(AdmobGender.MALE);
		string[] keywords = { "game", "crash", "male game" };
		ad.setKeywords(keywords);
		Debug.Log("admob inited -------------");
		DontDestroyOnLoad (this.gameObject);
	}

	public void Show()
	{
		//long tick = System.DateTime.Now.Ticks;
		//var ran = new System.Random((int)(tick & 0xffffffffL) | (int)(tick >> 32));
		//var value = ran.Next(0, 1000) > 500;
		//if (!value)
		//	return;
		Debug.LogError("显示广告");
		if (ad.isInterstitialReady())
		{
			ad.showInterstitial();
		}
		else
		{
			ad.loadInterstitial();
		}
	}
	
	void onInterstitialEvent(string eventName, string msg)
	{
		Debug.Log("handler onAdmobEvent---" + eventName + "   " + msg);
		if (eventName == AdmobEvent.onAdLoaded)
		{
			Admob.Instance().showInterstitial();
		}
	}
	void onBannerEvent(string eventName, string msg)
	{
		Debug.Log("handler onAdmobBannerEvent---" + eventName + "   " + msg);
	}
	void onRewardedVideoEvent(string eventName, string msg)
	{
		Debug.Log("handler onRewardedVideoEvent---" + eventName + "   " + msg);
	}
	void onNativeBannerEvent(string eventName, string msg)
	{
		Debug.Log("handler onAdmobNativeBannerEvent---" + eventName + "   " + msg);
	}
}
